define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/appointment/event-form.html'
  // 'bootstrapWysihtml5',
  ], function($, _, Backbone, template) {

  return Backbone.View.extend({
    template : _.template(template),
    events: {
//      'click button[name=save]'   : 'save',
//      'click button[name=cancel]' : 'cancel'
    },

    initialize: function(options) {
      //_.bindAll(this, 'render','close','success','error');
      //_.bindAll(this, 'render');
        _.bindAll(this);
		
    },

    render: function(){
      //this.$el.html(this.template());
        $(this.el).html(this.template);
        return this;

    },

      renderPopup:function(){
          $(this.el).dialog({
              modal: true,
              title:  (this.model.isNew() ? 'New' : 'Edit') + ' Appointment',
              buttons: {'Ok': this.save, 'Cancel': this.close},
              open: this.open
          });
          return this;
      },

      open: function() {
          this.$('#title').val(this.model.get('title'));
          this.$('#assigned_to').val(this.model.get('assigned_to'));
      },

      save: function() {
          this.model.set({
			'title': this.$('#title').val(), 
			'assigned_to': this.$('#assigned_to').val()
			});
		var self=this;
		  if (this.model.isNew()) {
			  app.appointmentList.create(this.model, {
				success: this.close,
				wait: true
				});
		  } else {
			  this.model.save({}, {success: this.close});
		  }
		  
      },

      close: function() {
         $(this.el).dialog('close');
      }
  });
});
